package com.test.realcityalfa.test;

import java.net.SocketTimeoutException;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.test.realcityalfa.R;
import com.test.realcityalfa.beans.BeanInterface;
import com.test.realcityalfa.frmwrk.DrawableManager;

public class CustomAdListAdapter extends ArrayAdapter<BeanInterface> {

	private ArrayList<BeanInterface> items;
	private DrawableManager imageManager;
	private Activity _activity;
	
	public CustomAdListAdapter(Activity activity, int textViewResourceId, ArrayList<BeanInterface> items) 
	{
		super(activity.getApplicationContext(), textViewResourceId, items);
		this.items = items;
		this._activity = activity;
		imageManager = initiateDrawableManager();
	}
	
	private DrawableManager initiateDrawableManager()
	{
		return new DrawableManager(_activity.getApplicationContext()) {
			
			@Override
			public void handleLoadingFinished(Object object) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void handleConnectionTimeout(SocketTimeoutException ex) {
				// TODO Auto-generated method stub
				
			}
		};
	}
 
	private String getImageURL(String pictureId)
	{
		//TODO imlement logic of image URL creation with different sizes
		return "http://media.realcity.cz/files/mobile/14767/img_4fdb17b029a5f8.06860346.jpg";
		//return Constants.URL_IMAGE_BASE + Constants.URL_IMAGE_SUFFIX + "?id=" + pictureId + "&key=ad";
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) throws NumberFormatException{
		// TODO Auto-generated method stub
		View v = convertView;
		ViewHolder holder;
		if(v == null)
		{			
			LayoutInflater li = (LayoutInflater)_activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);			
			v = li.inflate(R.layout.result_ad_item, null);
			
			holder = new ViewHolder();
			holder.pictCount = (TextView) v.findViewById(R.id.adItemImageCount);
			holder.pictureThumb = (ImageView) v.findViewById(R.id.adItemImage);
			holder.imageLoadProgress = (ProgressBar) v.findViewById(R.id.adItemImageLoadProgress);
			
			holder.title = (TextView) v.findViewById(R.id.adItemTitle);
			holder.price = (TextView) v.findViewById(R.id.adItemPrice);
			holder.priceUnit = (TextView) v.findViewById(R.id.adItemPriceUnit);
			holder.distance = (TextView) v.findViewById(R.id.adItemDistance);						
			
			v.setTag(holder);
			
		}
		else
		{
			//Log.d("TEST", "reciclyng view: " + v.getTag().getClass().getName());
			holder = (ViewHolder) v.getTag();
		}
		
		final BeanInterface item = items.get(position);
		if(item != null)
		{
			/*
			holder.disposition.setText(GetStringForId.getDisposition(Integer.valueOf(item.getNrOfRooms())));
			holder.locality.setText(item.getLocality());
			holder.post_code.setText(item.getPostCode());
			holder.price.setText(item.getPrice());
			holder.priceUnit.setText(item.getPriceUnit());
			holder.property_type.setText(GetStringForId.getPropertyType(item.getPropertyType()));
			holder.transaction_type.setText(GetStringForId.getTransactionType(item.getTransactionType()));
			holder.pict_count.setText(String.valueOf(item.getPictureCount()) + getContext().getResources().getString(R.string.layout_photo_count_suffix));			
			
			holder.ad_id = String.valueOf(item.getId()); // used to pass to next activity to load data
			holder.image.setTag(getImageURL(item.getPictureId()));
			
			
			imageManager.displayImage(getImageURL(item.getPictureId()), activity, holder.pictureThumb, holder.imageLoadProgress);
			*/
		}
		return v;
	}
	
	public static class ViewHolder
	{
		public TextView title,price,priceUnit,pictCount,distance;
		public ImageView pictureThumb;
		public ProgressBar imageLoadProgress;
		public String advetiseId;
	}
}
